package com.ruoyi.community.controller.wxApi;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.HealthyReport;
import com.ruoyi.community.domain.User;
import com.ruoyi.community.service.IHealthyReportService;
import com.ruoyi.community.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 健康填报Controller
 *
 * @author lj
 * @date 2022-03-16
 */
@RestController
@RequestMapping("/wx/community/report")
@Api(tags = {"健康填报"})
public class WxHealthyReportController extends BaseController
{
    @Autowired
    private IHealthyReportService healthyReportService;
    @Autowired
    private IUserService userService;

    /**
     * 查询健康填报列表
     */
    @ApiOperation(value = "查询健康填报列表")
    @GetMapping("/list")
    public TableDataInfo list(HealthyReport healthyReport)
    {
        startPage();
        List<HealthyReport> list = healthyReportService.selectHealthyReportList(healthyReport);
        return getDataTable(list);
    }

    /**
     * 导出健康填报列表
     */
    @ApiOperation(value = "导出健康填报列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, HealthyReport healthyReport)
    {
        List<HealthyReport> list = healthyReportService.selectHealthyReportList(healthyReport);
        ExcelUtil<HealthyReport> util = new ExcelUtil<HealthyReport>(HealthyReport.class);
        util.exportExcel(response, list, "健康填报数据");
    }

    /**
     * 获取健康填报详细信息
     */
    @ApiOperation(value = "获取健康填报详细信息")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(healthyReportService.selectHealthyReportById(id));
    }

    /**
     * 新增健康填报
     */
    @ApiOperation(value = "新增健康填报")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody HealthyReport healthyReport)
    {
        User user = userService.selectUserById(healthyReport.getUserId());
        healthyReport.setUserName(user.getUsername());
        healthyReport.setPhonenumber(user.getPhonenumber());
        return toAjax(healthyReportService.insertHealthyReport(healthyReport));
    }

    /**
     * 修改健康填报
     */
    @ApiOperation(value = "修改健康填报")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody HealthyReport healthyReport)
    {
        return toAjax(healthyReportService.updateHealthyReport(healthyReport));
    }

    /**
     * 删除健康填报
     */
    @ApiOperation(value = "删除健康填报")
	@DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {      Long[] ids= {id};
        return toAjax(healthyReportService.updateHealthyReportByIds(ids));
    }

    /**
     * 判断用户当天有没有填写数据
     */
    @ApiOperation(value = "判断用户当天有没有填写数据")
    @GetMapping(value = "/judg")
    public AjaxResult judg(Long userId)
    {
        return AjaxResult.success(healthyReportService.selectHealthyReportByUserId(userId));
    }

    @ApiOperation(value = "获取当天填写的数据")
    @GetMapping(value = "/nowInfo")
    public AjaxResult nowInfo(Long userId)
    {
        return AjaxResult.success(healthyReportService.selectHealthyReportByUser(userId));
    }
}
